export function GMTToStr(time){
    let date = new Date(time)
    let Str=date.getFullYear() + '-' +
    (date.getMonth() + 1) + '-' + 
    date.getDate() + ' ' + 
    date.getHours() + ':' + 
    date.getMinutes() + ':' + 
    date.getSeconds()
    return Str
}
export function flatArr(arr,newArr){
    if(arr.length <= 0){
        return;
    }else{
        for(let i=0;i<arr.length;i++){
            newArr.push(arr[i]);
            if(arr[i].children.length>0){
                flatArr(arr[i].children,newArr);
            }
        }
        return newArr;
    }
}
/** 时间格式化
 * @param {Date|Number} time 时间对象或毫秒数
 * @param {String} template 时间格式模板，不传返回默认时间格式。模板格式例子：`{YY}-{MM}-{DD} {h}:{m}:{s}`
 * @return {String}
 */

export function timeFormat(time, template) {
    const date = new Date(time)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const min = date.getMinutes()
    const second = date.getSeconds()

    let newTime = template || `${year}-${month}-${day} ${hour}:${min}:${second}`
    if (template) {
        const formatTypes = {
            '{YYYY}': format.bind(null, year),
            '{YY}': format.bind(null, year, true),
            '{MM}': format.bind(null, month, true),
            '{M}': format.bind(null, month),
            '{DD}': format.bind(null, day, true),
            '{D}': format.bind(null, day),
            '{hh}': format.bind(null, hour, true),
            '{h}': format.bind(null, hour),
            '{mm}': format.bind(null, min, true),
            '{m}': format.bind(null, min),
            '{ss}': format.bind(null, second, true),
            '{s}': format.bind(null, second)
        }
        const reg = /(\{YYYY\})?(\{YY\})?(\{MM\})?(\{M\})?(\{DD\})?(\{D\})?(\{hh\})?(\{h\})?(\{mm\})?(\{m\})?(\{ss\})?(\{s\})?/g
        newTime = newTime.replace(reg, (val) => {
            const fuc = formatTypes[val]
            return typeof fuc === 'function' ? fuc() : val
        })
    }
    return newTime

    /**
     * 获取格式化后的时间
     * @param {Number} num 时间数字
     * @param {Boolean} isKeep 是否保留两位
     */
    function format(num, isKeep) {
        let val = num
        if (isKeep) {
            const str = String(num)
            val = str.length > 2 ? str.substring(str.length - 2)
                : str.length < 2 ? '0' + str : str
        }
        return val
    }
}
